package com.peas.platform.module.icims.base.mdm.domain.impl;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.base.mdm.adapter.repository.BankRepository;
import com.peas.platform.module.icims.base.mdm.adapter.repository.po.BankPo;
import com.peas.platform.module.icims.base.mdm.domain.BankDomain;
import com.peas.platform.module.icims.base.mdm.dto.BankDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class BankDomainImpl extends BaseDomainImpl<BankDto, BankPo> implements BankDomain {

    @Resource
    private BankRepository bankRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<BankPo> getMapper() {
        return bankRepository;
    }

    @Override
    public Class<? extends BaseRepository<BankPo>> getRepositoryClass() {
        return BankRepository.class;
    }

    @Transactional
    @Override
    public int save(BankDto dto) {
        if (StrUtil.isBlank(dto.getBankCode())) {
            String code = sequenceService.genSequence("base", "BankDto", "BD", 4, 1);
            dto.setBankCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("bankCode", dto.getBankCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(BankDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("bankCode", dto.getBankCode());
        return result;
    }

}
